#!/usr/bin/env bash

set -euo pipefail

UNAME="$(uname -s)"
if [[ "${UNAME}" != "Linux" && "${UNAME}" != "Darwin" ]]; then
  echo "linux_fifo_smoke.sh is intended for Linux or macOS hosts" >&2
  exit 0
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

CLI_BIN="${REPO_ROOT}/build/asycipcctl"
if [[ ! -x "${CLI_BIN}" ]]; then
  echo "asycipcctl binary not found at ${CLI_BIN}" >&2
  echo "Please build the CLI first: cmake --build build --target asycipcctl" >&2
  exit 1
fi

TEMP_DIR="$(mktemp -d /tmp/asycipc_cli_XXXXXX)"
cleanup() {
  rm -rf "${TEMP_DIR}"
}
trap cleanup EXIT

echo "[+] Using temporary endpoint directory ${TEMP_DIR}" >&2

"${CLI_BIN}" sessions list "${TEMP_DIR}"

mkfifo "${TEMP_DIR}/session-test-in"
mkfifo "${TEMP_DIR}/session-test-out"

"${CLI_BIN}" sessions list "${TEMP_DIR}"

"${CLI_BIN}" transport cleanup "${TEMP_DIR}" --force

echo "[+] Smoke test completed" >&2
